<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">新增日程</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<scroll-view style="height: 390px;" scroll-y="true">
				<uni-forms ref="baseForm" :modelValue="baseFormData">
					<uni-forms-item label="日程主题:" labelAlign="right" required>
						<uni-easyinput type="textarea" v-model="baseFormData.name" placeholder="请填写" />
					</uni-forms-item>

					<uni-forms-item label="开始时间:" labelAlign="right" required>
						<view style="display: flex;">
							<view class="date-select-back" @click="openDatetimePicker(0)">
								<image class="dateimage" src="../../../static/pc_date.png"></image>
								<view class="date-select-time">
									{{baseFormData.startDate}}
								</view>
							</view>
							<view class="date-select-back" v-if="!isAllDay" style="margin-left:8px ;"
								@click="openDatetimePicker(0)">
								<image class="dateimage" src="../../../static/pc_schedul_time.png"></image>
								<view class="date-select-time">
									{{baseFormData.startTime}}
								</view>
							</view>
						</view>

					</uni-forms-item>
					<uni-forms-item label="结束时间:" labelAlign="right" required>
						<view style="display: flex;">
							<view class="date-select-back" @click="openDatetimePicker(1)">
								<image class="dateimage" src="../../../static/pc_date.png"></image>
								<view class="date-select-time">
									{{baseFormData.endDate}}
								</view>
							</view>
							<view class="date-select-back" v-if="!isAllDay" style="margin-left:8px ;"
								@click="openDatetimePicker(1)">
								<image class="dateimage" src="../../../static/pc_schedul_time.png"></image>
								<view class="date-select-time">
									{{baseFormData.endTime}}
								</view>
							</view>
							<view style="margin-left:8px ;">
								<uni-data-checkbox selectedColor="#2DA9C5" @change="alldayChange"
									v-model="baseFormData.isAllday" multiple :localdata="hobbys" />
							</view>
						</view>

					</uni-forms-item>
					<uni-forms-item label="日程提醒:" labelAlign="right" required>
						<view style="width: 270px;">
							<uniselect v-model="baseFormData.tipsTimes" :localdata="remindTimeList" @change="change"
								:clear="false">
							</uniselect>
						</view>

					</uni-forms-item>
					<uni-forms-item label="参与人:" labelAlign="right" required>
						<uni-data-checkbox selectedColor="#2DA9C5" v-model="baseFormData.people" :localdata="peopleArr">
						</uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item label="所属案件:" labelAlign="right" required>
						<view>
							<view class="addBtn" @click="clisckSelectAction">
								{{baseFormData.caseId?'重新选择':'选择'}}
							</view>
							<view class="item-item-back" v-if="baseFormData.caseId">
								<view class="item-top-back">
									<image class="item-top-img" src="../../../static/phone/phone_case_list_img.png">
									</image>
									<view class="item-name">{{caseData.custName}}</view>
									<view class="item-num">编号: {{caseData.no}}</view>
								</view>
								<view class="item-content">
									{{caseData.content}}
								</view>
								<view class="item-status">状态:<text class="item-status"
										style="color: #2DA9C5;">{{caseData.status.code=='4' ? caseData.bizStatus : caseData.status.desc}}</text>
								</view>
							</view>
						</view>
					</uni-forms-item>

				</uni-forms>
			</scroll-view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>

				<view class="bottom-btn" @click="saveIt">保存</view>
			</view>
		</view>
		<dateTimeSelect ref="myPicker" @submit="handleSubmit" :start-year="2020" :end-year="2025" :type='dateType' />
		<caselSelect v-if="showSelect" @caseSelectClose='clickCaseSelectClose'
			@clickCustomerAction="clickCustomerAction"></caselSelect>
	</view>
</template>

<script>
	import dateTimeSelect from "@/components/dateTimeSelect/dateTimeSelect.vue"
	import caselSelect from "@/packageC/PhonePC/schedule/caseSelect.vue"
	import uniselect from "@/packageC/PhonePC/components/uni-data-select/uni-data-select.vue"
	export default {
		props: ['types', 'eventActionDateDetail'],
		components: {
			dateTimeSelect,
			caselSelect,
			uniselect
		},
		data() {
			return {
				role: '',
				roleUrl: '',
				baseFormData: {
					'id':'',
					'name': '',
					'startDate': '',
					'startTime': '',
					'endDate': '',
					'endTime': '',
					'isAllday': '',
					'people': '',
					'caseId': '',
					'tipsTimes': ''
				},
				dateSelectIndex: 0,
				dateType: 'datetime',
				hobbys: [{
					text: '全天',
					value: 1
				}],
				isAllDay: false,
				peopleArr: [{
					text: '全体参与者',
					value: '0'
				}, {
					text: '仅承办律师',
					value: '1'
				}],
				showSelect: false,
				caseData: null,
				remindTimeList: [{
						value: "0",
						text: '无提醒',
					},
					{
						value: "1",
						text: '日程开始时提醒',
					},
					{
						value: "2",
						text: '15分钟前提醒',
					},
					{
						value: "3",
						text: '30分钟前提醒',
					},
					{
						value: "4",
						text: ' 一小时前提醒',
					},
					{
						value: "5",
						text: '一天前提醒',
					},
					{
						value: "6",
						text: '一天前提醒',
					},
					{
						value: "7",
						text: '1天前9点提醒',
					},
					{
						value:"8",
						text: '2天前9点提醒',
					},
				],

			}
		},
		onLoad() {

		},
		mounted() {
			console.log('查询案件'+ this.eventActionDateDetail)
			this.role = getApp().globalData.role
			this.roleUrl = this.$roleUrl({
				tab: this.role
			})
			if (this.types == 'edit') {
				this.baseFormData = {
					'id':this.eventActionDateDetail.id,
					'name': this.eventActionDateDetail.title,
					'startDate': this.eventActionDateDetail.beginTime.split(' ')[0],
					'startTime': this.eventActionDateDetail.beginTime.split(' ')[1],
					'endDate': this.eventActionDateDetail.endTime.split(' ')[0],
					'endTime': this.eventActionDateDetail.endTime.split(' ')[1],
					'isAllday': this.eventActionDateDetail.allDay.code,
					'people': this.eventActionDateDetail.joinType.code,
					'caseId': this.eventActionDateDetail.caseId,
					'tipsTimes': this.eventActionDateDetail.tipsTimes[0].code
				}
				this.caseData = this.eventActionDateDetail.cases
			}
		},

		methods: {
			//点击选择
			clisckSelectAction() {
				this.showSelect = true;
			},
			clickCaseSelectClose() {
				this.showSelect = false;
			},
			clickCustomerAction(data) {
				this.showSelect = false;
				console.log(data)
				this.caseData = data
				this.baseFormData.caseId = data.id
			},

			// 日期选择打开picker
			openDatetimePicker(index) {
				this.dateSelectIndex = index;
				this.$refs.myPicker.show();
			},
			handleSubmit(e) {
				// {year: "2019", month: "07", day: "17", hour: "15", minute: "21"}
				console.log(e);
				var date = `${e.year}-${e.month}-${e.day}`;
				var time = `${e.hour}:${e.minute}`;
				if (this.dateSelectIndex == 0) {
					this.baseFormData.startDate = date;
					this.baseFormData.startTime = time;
				} else {
					this.baseFormData.endDate = date;
					this.baseFormData.endTime = time;
				}
			},
			alldayChange(e) {
				console.log(e)
				if (e.detail.value.length == 0) {
					this.isAllDay = false
					this.dateType = 'datetime'
				} else {
					this.isAllDay = true
					this.dateType = 'date'
				}
			},
			closeBtnAction() {
				this.$emit('addClose')

			},
			moveHandle() {
				return true
			},
			//新增
			async saveIt() {
				let _this = this
				if (_this.baseFormData.name == '') {
					_this.showToastEmpty("日程主题不能为空!")
					return
				}
				if (_this.baseFormData.startDate == '') {
					_this.showToastEmpty("请选择开始时间!")
					return
				}

				if (_this.baseFormData.endDate == '') {
					_this.showToastEmpty("请选择结束时间!")
					return
				}
				if (_this.baseFormData.tipsTimes == '') {
					_this.showToastEmpty("请选择日程提醒!")
					return
				}
				if (_this.baseFormData.people == '' && _this.baseFormData.people != '0') {
					_this.showToastEmpty("请选择参与人类型!")
					return
				}
				if (_this.baseFormData.caseId == '') {
					_this.showToastEmpty("请选择所属案件!")
					return
				}
				
			


				let allDay = ''
				if (_this.baseFormData.isAllday[0] == 1) {
					allDay = '1'
				} else {
					allDay = '0'
				}
				
				let data = {
					id:_this.baseFormData.id,
					caseId: _this.baseFormData.caseId,
					title: _this.baseFormData.name,
					beginTime: _this.baseFormData.startDate + ' ' + _this.baseFormData.startTime,
					endTime: _this.baseFormData.endDate + ' ' + _this.baseFormData.endTime,
					allDay: allDay,
					joinType: _this.baseFormData.people,
					content: '无',
					tipsTimes: _this.baseFormData.tipsTimes
				}
				var time1 = new Date(data.beginTime).getTime()
				var time2 = new Date(data.endTime).getTime()
				if(time1 >= time2){
					_this.showToastEmpty("开始时间必须早于结束时间!")
					return
				}
				
				uni.showLoading({
					title: '正在提交...'
				})
				console.log('上传参数', data)
				console.log(this.types)
				let method = 'POST'
				if (_this.types == 'edit') {
					
					method = 'PUT'
				}
				const res = await _this.$myRequest({
					url: `/${_this.roleUrl}/caseTodo`,
					method: method,
					data: data
				})
				uni.hideLoading()
				if (res.data.code == 200) {
					uni.showToast({
						title: this.types == 'edit' ? '编辑成功' : '添加成功'
					})
					_this.$emit("successBack");
					_this.closeBtnAction()
				}
			},

			showToastEmpty(val) {
				uni.showToast({
					title: `${val}`,
					icon: 'none'
				})
			},
			change(e) {
				console.log("e:", e);
				this.baseFormData.tipsTimes = e
			},

		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 3;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 66;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 560px;
		height: 500px;
		padding: 24px;
		max-height: 500px;
		background-color: white;
		margin-top: -50px;
		border-radius: 4px;
		z-index: 98;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;
	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {

		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}


	.bottom-btn-cancel {
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}

	.date-select-back {
		width: 130px;
		height: 32px;
		background: #FFFFFF;
		border-radius: 2px;
		border: 1px solid #EEEEEE;
		display: flex;
		align-items: center;
	}

	.dateimage {
		width: 16px;
		height: 16px;
		margin-left: 10px;
		margin-right: 3px;
	}

	.date-select-plac {
		font-size: 12px;
		color: #999999;
	}

	.date-select-time {
		font-size: 12px;
		color: #333333;
	}

	.addBtn {
		width: 104px;
		height: 30px;
		background: #E8F7FB;
		border-radius: 2px;
		font-size: 14px;
		font-weight: normal;
		color: #2DA9C5;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.item-item-back {
		border-radius: 2px;
		border: 1px solid #EEEEEE;
		width: 450px;
		padding: 10px 16px;
		margin-top: 8px;
	}

	.item-top-back {
		display: flex;
		align-items: center;
	}

	.item-top-img {
		width: 24px;
		height: 24px;
	}

	.item-name {
		font-size: 12px;
		font-weight: 400;
		color: #333333;
		margin-left: 6px;
	}

	.item-num {
		font-size: 12px;
		font-weight: 400;
		color: #999999;
		margin-right: 0px;
		margin-left: auto;
	}

	.item-content {
		font-size: 14px;
		font-weight: 400;
		color: #333333;
		margin-top: 8px;
	}

	.item-status {
		font-size: 12px;
		font-weight: 400;
		color: #333333;
		margin-top: 8px;
	}
</style>
